using Spine.Unity;
using System;
using System.Collections;
using UnityEngine;

public class Miner : MonoBehaviour
{
	private Animator animator;

	private MineShaft mineShaft;

	private Vector3 gatheringPoint;

	private Vector3 exploitedPoint;

	private Vector3 restingPosition;

	private Transform myselfTransform;

	private Transform animatorTransform;

	private int skinID;

	private float miningTime;

	private float walkingSpeed;

	private float skinEffective;

	private bool isIdle = true;

	public bool IsIdle => isIdle;

	private void Awake()
	{
		myselfTransform = base.transform;
		restingPosition = myselfTransform.position;
		animator = GetComponentInChildren<Animator>();
		animatorTransform = animator.transform;
	}

	public void Initialize(MineShaft mineShaft)
	{
		this.mineShaft = mineShaft;
		gatheringPoint = mineShaft.GatheringPoint;
		Vector3 position = base.transform.position;
		Vector3 right = Vector3.right;
		Vector3 vector = mineShaft.ExploitedPoint;
		float x = vector.x;
		Vector3 vector2 = mineShaft.GatheringPoint;
		exploitedPoint = position + right * (x - vector2.x);
		Collectible instance = Collectible.instance;
		instance.changeMinerSkin = (Action<int>)Delegate.Combine(instance.changeMinerSkin, new Action<int>(ChangeSkin));
		Collectible.Properties property = Collectible.instance.Property;
		ChangeSkin(property.minerSkin);
	}

	private void ChangeSkin(int skinID)
	{
		this.skinID = skinID;
		switch (skinID)
		{
		case 0:
			GetComponentInChildren<SkeletonAnimator>().skeleton.SetSkin("Miner1");
			break;
		case 1:
			GetComponentInChildren<SkeletonAnimator>().skeleton.SetSkin("miner1Epic");
			break;
		case 2:
			GetComponentInChildren<SkeletonAnimator>().skeleton.SetSkin("miner1Legend");
			break;
		}
		skinEffective = Collectible.instance.GetSkinEffective(Collectible.SkinTYPE.Miner);
	}

	public void StartMining()
	{
		isIdle = false;
		StartCoroutine(Mining());
	}

	private void Animate(string clip, float speed)
	{
		if (clip != null)
		{
			if (!(clip == "Walk") && !(clip == "Collect"))
			{
				if (clip == "Pick" || clip == "Digging")
				{
					MineShaft.Properties property = mineShaft.Property;
					double workerCapacity = property.workerCapacity;
					MineShaft.Properties property2 = mineShaft.Property;
					miningTime = (float)(workerCapacity / (property2.miningSpeed * (double)speed));
				}
			}
			else
			{
				MineShaft.Properties property3 = mineShaft.Property;
				walkingSpeed = property3.walkingSpeed * speed;
			}
		}
		animator.speed = speed;
		animator.Play(clip);
	}

	private IEnumerator Mining()
	{
		MineShaft.MineShaftBoost boosts = mineShaft.Boosts;
		Animate("Walk", boosts.walkingSpeed);
		while (myselfTransform.position != exploitedPoint)
		{
			myselfTransform.position = Vector3.MoveTowards(myselfTransform.position, exploitedPoint, Time.deltaTime * walkingSpeed);
			yield return null;
		}
		string clip = (skinID != 1) ? "Digging" : "Pick";
		MineShaft.MineShaftBoost boosts2 = mineShaft.Boosts;
		Animate(clip, boosts2.miningSpeed);
		yield return new WaitForSeconds(miningTime);
		animatorTransform.eulerAngles += Vector3.up * 180f;
		MineShaft.MineShaftBoost boosts3 = mineShaft.Boosts;
		Animate("Collect", boosts3.walkingSpeed);
		while (myselfTransform.position != gatheringPoint)
		{
			myselfTransform.position = Vector3.MoveTowards(myselfTransform.position, gatheringPoint, Time.deltaTime * walkingSpeed);
			yield return null;
		}
		animatorTransform.eulerAngles += Vector3.up * 180f;
		MineShaft obj = mineShaft;
		MineShaft.Properties property = mineShaft.Property;
		obj.SetCash(property.workerCapacity * (double)skinEffective);
		if (mineShaft.Profiles == null)
		{
			MineShaft.MineShaftBoost boosts4 = mineShaft.Boosts;
			Animate("Walk", boosts4.walkingSpeed);
			while (myselfTransform.position != restingPosition)
			{
				myselfTransform.position = Vector3.MoveTowards(myselfTransform.position, restingPosition, Time.deltaTime * walkingSpeed);
				yield return null;
			}
			if (mineShaft.Profiles == null)
			{
				isIdle = true;
				mineShaft.IdleMiner();
				Animate("Idle", 1f);
			}
			else
			{
				StartCoroutine(Mining());
			}
		}
		else
		{
			StartCoroutine(Mining());
		}
	}
}
